﻿package game.objects {
	import game.global.Global;
	import game.lang.Lang;
	import game.context.Context;
	import game.debug.Debug;
	import game.socket.Sockets;
	import game.network.Network;
	import game.libs.Libs;
	import game.errors.GameError;
	import game.Config;
	import game.music.Music;
	import flash.display.Stage3D;
	import flash.display.MovieClip;
	import flash.geom.Rectangle;
	import fl.transitions.Tween;
	import fl.transitions.TweenEvent;
	import fl.transitions.easing.Regular;
	import flash.events.Event;
	import flash.utils.getQualifiedClassName;
	import flash.display3D.Context3D;
	
	public class GameObject extends MovieClip {
		
		public var isAdd:Boolean = false;
		
		public function GameObject():void {
			this.addEventListener(Event.ADDED_TO_STAGE, added);
			this.addEventListener(Event.REMOVED_FROM_STAGE, removed);
			
		}
		
		
		
		private function added(event:Event):void { isAdd = true; }
		
		private function removed(event:Event):void { isAdd = false; }
		
		public function deleteAll():void { while (this.numChildren) removeChildAt(0); }
		
		public function set libs(value:Libs):void { global.libs = value; }
		
		public function get libs():Libs { return global.libs as Libs; }
		
		public function set network(value:Network):void { global.network = value; }
		
		public function get network():Network { return global.network as Network; }
		
		public function set socket(value:Sockets):void { global.socket = value; }
		
		public function get socket():Sockets { return global.socket as Sockets; }
		
		public function set music(value:Music):void { global.music = value; }
		
		public function get music():Music { return global.music as Music; }
		
		public function debug(text:String, type:String = ""):void { Debug.log(text, type); }
		
		public function set context(value:Context):void { global.context = value; }
		
		public function get context():Context { return global.context as Context; }
		
		public function set lang(value:Lang):void { global.lang = value; }
		
		public function get lang():Lang { return global.lang as Lang; }
		
		public function set config(value:Config):void { global.config = value; };
		
		public function get config():Config { var conf:Config; if (!global) conf = new Config; else if (!global.config) { conf = new Config; global.config = conf; } else conf = global.config as Config; return conf; }
		
		public function set stage3D(value:Stage3D) { global.stage3d = value; }
		
		public function get stage3D():Stage3D { return global.stage3d; }
		
		public function set context3D(value:Context3D) { global.context3d = value; }
		
		public function get context3D():Context3D { return global.context3d; }
		
		public function draw (rect:Rectangle, color:uint = 0xff0000, alpha:Number = 1) { this.graphics.clear(); this.graphics.beginFill(color, alpha); this.graphics.drawRect(rect.x, rect.y, rect.width, rect.height); this.graphics.endFill(); }
		
		public function error(type:String, id:Number, string:String = "", params:Object = null):void {
			try {
				var error:GameError = new GameError (type, id, string, params);
				addChild(error);
			} catch (e:Error) {
				trace (type, id);
			}
		}
		
		/* анимация */ private var tweens:Array = new Array; public function animate(prop:String, begin:Number, finish:Number, time:Number = 1, func:Function = null) { try { var tween:Tween = new Tween(this, prop, fl.transitions.easing.Regular.easeOut, begin, finish, time, true); tweens.push(tween); } catch (e:Error) { trace(e.message); }  if (func!=null) tween.addEventListener(TweenEvent.MOTION_FINISH, func); }
		
		private function get global():Global { return Global.getInstance(); }
		
		public static function formatToString(object:*, ...properties:Array):String {
			var c:String = getQualifiedClassName(object);
			c = c.substr(c.lastIndexOf(':')+1);
			var s:String='['+c;
			var prop:String;
			for (var i:int=0; i<properties.length; i++) {
				prop=properties[i];
				s+=' '+prop+'('+object[prop]+')';
			}
			return s+']';
        }
	}
}